package com.music.controller;

import com.alibaba.druid.util.StringUtils;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.util.StringUtil;
import com.music.dao.AlbumMapper;
import com.music.dao.SingerMapper;
import com.music.dto.Result;
import com.music.entity.Album;
import com.music.service.AlbumService;
import com.music.service.impl.AlbumServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Optional;

/**
 * 肖泽强
 */
@Controller
@RequestMapping("/album")
@Slf4j
public class AlbumController {
    @Autowired
    private AlbumService albumService1;
    @Autowired
    private AlbumMapper albumMapper;


    @GetMapping("/get/{label}")
    public @ResponseBody Result getAlbum(@PathVariable String label,String size,String districtId) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InstantiationException {
        Result<Object> result = new Result<>();
        boolean flag = true;
        if(StringUtil.isEmpty(size)){
            size = "5";
        }else{
            if(!StringUtils.isNumber(size)){
                flag = false;
            }
        }

        if(StringUtil.isEmpty(districtId)){
            districtId = "0";
        }else{
            if(!StringUtils.isNumber(districtId)){
                flag = false;
            }
        }


        if(!StringUtil.isEmpty(label.trim()) && flag){
            HashMap<String, Object> map = new HashMap<>();

            Class<?> c = Class.forName("com.music.service.impl.AlbumServiceImpl");
            AlbumServiceImpl albumService = (AlbumServiceImpl) c.newInstance();
            try {
                Field field = c.getDeclaredField("albumMapper");
                field.setAccessible(true);
                field.set(albumService,albumMapper);
            } catch (NoSuchFieldException e) {
                e.printStackTrace();
            }


            List<Album> list = null;
            switch (label){
                case "NewPutaway":
                    //true 查询最新  false !
                    try {
                        Method method = c.getDeclaredMethod("getNewPutawayForFive", boolean.class, Integer.class);
                        list = (List<Album>) method.invoke(albumService,true,Integer.valueOf(size));
                    } catch (InvocationTargetException e) {
                        e.printStackTrace();
                    }
                    break;
                case "HotAlbum":
                    try {
                        Method method = c.getDeclaredMethod("getHotAlbum", boolean.class, Integer.class, Integer.class);
                        list = (List<Album>) method.invoke(albumService,true,Integer.valueOf(size),Integer.valueOf(districtId));

                    } catch (InvocationTargetException e) {
                        e.printStackTrace();
                    }
                    break;
                default:
                    list = albumService1.getAlbumByConditions(map);
            }

            if(list == null || list.isEmpty()){
               result.fail(0,"没查询到任何数据");
            }else{
                result.ok(list).setMessage("查询成功");
            }
        }else{
            result.fail(0,"参数不正确");
        }


        return result;
    }

    @RequestMapping("/all")
    public @ResponseBody Result dogetAlbumList(){
        Result result=new Result();
        List<Album> list=new ArrayList<Album>();
        Album album=new Album();
        list=albumService1.findAlbumList(album);
        if(!list.isEmpty()){
            result.ok(list);
        }else{
            result.fail(0,"没有相关数据");
        }
        return result;
    }

}
